<?php

namespace Home\Controller;

use \Home\Controller\BaseController;

class TeacherController extends BaseController {

    /**
     * 老师列表
     * @param int $sid 店铺ID
     */
    public function teacherlist() {
        $sid      = I("sid",0,"intval");
        $techList = M("Teacher")->Field("tid,name,face,shop_id")->where(array("shop_id" => $sid))->order("tid asc")->select();
        if($techList){
            $this->ajaxReturn(array('error' => 0,'info' => $techList));
        }else{
            $this->ajaxReturn(array('error' => 0,'info' => '没有相关老师'));
        }
        
    }

    /**
     * 店长-选择上班老师
     * @param string $tids 老师id（多个逗号分隔）
     */
    public function choice() {
        $tids = I("tids");
        if(empty($tids))
            $this->ajaxReturn(array('error' => 1,'info' => '没有选择老师'));

        $arrTid = explode(',',str_replace("，",",",$tids));//防止中文逗号

        $nowDate = date("Y-m-d");
        $nowTime = time();

        //删除当天已选中的，重新添加。
        M("Course")->where(array('class_date' => $nowDate))->delete();

        foreach($arrTid as $key => $val) {
            $data[$key]['tid']        = $val;
            $data[$key]['class_date'] = $nowDate;
            $data[$key]['addtime']    = $nowTime;
        }
        $res = M("Course")->addAll($data);
        $this->ajaxReturn(array('error' => 0,'info' => '添加成功'));
    }

    /**
     * 显示当天老师评价数据（当天上班的老师）
     * @param int $sid 店铺ID
     */
    public function evallist() {
        $sid = I("sid",0,"intval");

        $todayStart = date("Y-m-d");
        $todatEnd   = date("Y-m-d",strtotime("+1 day"));

        //当天该分店上课老师
        $todayTeacher = M()->table("ot_course c")->field("t.tid,t.name,t.face")
                ->join("ot_teacher as t ON t.tid=c.tid")
                ->where(array('t.shop_id' => $sid,'c.class_date' => array('BETWEEN',array($todayStart,$todatEnd))))
                ->select();

        if(empty($todayTeacher)) {
            $this->ajaxReturn(array('error' => 1,'info' => '暂时没有数据'));
        }

        foreach($todayTeacher as $val) {
            $tids[] = $val['tid'];
        }
        //当天该分店对老师的评价
        $evalList = M("TeacherEval")->field("tid,score")->where(array('tid' => array('in',$tids),'addtime' => array('BETWEEN',array($todayStart,$todatEnd))))->select();

        foreach($todayTeacher as &$val) {
            $val['star1'] = 0;
            $val['star2'] = 0;
            $val['star3'] = 0;
            $val['star4'] = 0;
            $val['star5'] = 0;
            foreach($evalList as $vv) {
                if($val['tid'] == $vv['tid']) {
                    $_star = 'star' . $vv['score'];
                    $val[$_star] += 1;
                }
            }
        }
        unset($val);
        $this->ajaxReturn(array('error' => 0,'info' => $todayTeacher));
    }

    /**
     * 当天老师评价详情(是非题)
     */
    public function evalDetail() {
        $tid        = I("tid",0,"intval");
        $todayStart = date("Y-m-d");
        $todatEnd   = date("Y-m-d",strtotime("+1 day"));
        $evalList   = M("TeacherEval")->field("id,tid")->where(array('tid' => $tid,'addtime' => array('BETWEEN',array($todayStart,$todatEnd))))->select();
        if(empty($evalList)) {
            $this->ajaxReturn(array('error' => 1,'info' => '没有数据'));
        }
        foreach($evalList as $val) {
            $eids[] = $val['id'];
        }
        $evalIsList = M()->table("ot_teacher_eval_is tei")->field("tei.*,q.title")
                ->join("ot_question as q ON q.qid = tei.qid")
                ->where(array('tei.eid' => array('in',$eids)))
                ->select();

        $qList = M("Question")->field("qid,title")->where(array('is_hide'=>0))->order('qid asc')->select();
        foreach($qList as &$val) {
            $val['yes_num'] = 0;
            $val['no_num']  = 0;
            if(!empty($evalIsList)) {
                foreach($evalIsList as $vv) {
                    if($val['qid'] == $vv['qid']) {
                        $vv['yes_no'] == 1 ? ($val['yes_num']+=1) : ($val['no_num']+=1);
                    }
                }
            }
        }
        unset($val);
        $this->ajaxReturn(array('error' => 0,'info' => $qList));
    }

    /**
     * 是非题列表接口
     */
    public function questionList() {
        $qList = M("Question")->field("qid,title")->where(array('is_hide'=>0))->order('qid asc')->select();
        if($qList){
            $this->ajaxReturn(array('error' => 0,'info' => $qList));
        }else{
            $this->ajaxReturn(array('error' => 1,'info' => '暂时没有问题调查'));
        }
        
    }

    /**
     * 顾客-评价老师
     * @param int $tid 老师ID
     */
    public function evalteacher() {
        $data['tid']   = I('tid',0,"intval");
        $data['score'] = I('score',0,'intval');
        $q_res  = I("q_res");

        if(empty($data['tid']) || empty($data['score']) || empty($q_res)) {
            $this->ajaxReturn(array('error' => 1,'info' => '非法数据'));
        }

        $teacherInfo = M("Teacher")->where(array('tid'=>$data['tid']))->find();
        $data['sid'] = $teacherInfo['shop_id'];
        $eid = M("TeacherEval")->add($data);
        if($eid) {
            $q_res = json_decode($q_res,true);

            $i = 0;
            foreach($q_res as $val) {
                $edata[$i]['qid'] = $val['id'];
                $edata[$i]['eid'] = $eid;
                $edata[$i]['yes_no'] = $val['value'];
                $i++;
            }
            M("TeacherEvalIs")->addAll($edata);
            $this->ajaxReturn(array('error' => 0,'info' => '评价成功'));
        }
        else {
            $this->ajaxReturn(array('error' => 1,'info' => '评价失败'));
        }
    }

    /**
     * 当天所有老师评价列表
     */
    public function allEvalList() {
        $where['te.addtime'] = array('like',date("Y-m-d").'%');

        $list =  M()->table("ot_teacher_eval te")->field("te.*,t.name,s.shopname")
                    ->join("ot_teacher as t ON t.tid=te.tid")
                    ->join("ot_shop as s ON s.sid = te.sid")
                    ->where($where)
                    ->order("te.id desc")
                    ->select();
        
        if(empty($list)){
            $this->ajaxReturn(array('error' => 1,'info' => '暂无相关数据'));
        }

        foreach($list as $val){
            $eids[] = $val['id'];
        }

        //$evalIslist = M("TeacherEvalIs")->field("")->where(array('eid'=>array('in',$eids)))->select();
        $evalIslist = M()->table("ot_teacher_eval_is tei")->field("tei.eid,yes_no,title")
                        ->join("left join ot_question as q ON q.qid = tei.qid")
                        ->where(array('tei.eid'=>array('in',$eids)))
                        ->select();

        foreach($list as &$val){
            foreach($evalIslist as $vv){
                if($val['id'] == $vv['eid']){
                    $val['is_eval_list'][] = $vv;
                }
            }
        }
        
        $this->ajaxReturn(array('error' => 0,'info' => $list));
    }

}
